{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dc9d22f5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([8, 2])\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "import numpy as np\n",
    "from torch import nn\n",
    "import torch.nn.functional as f\n",
    "import math\n",
    "X=torch.tensor([[1., 2.],\n",
    "                [2., 1.],\n",
    "                [3., 5.],\n",
    "                [5., 3.],\n",
    "                [1., -1.],\n",
    "                [-1., 1.],\n",
    "                [-2., -3.],\n",
    "                [-3., -2.]])\n",
    "Y=torch.tensor([0,0,1,1,2,2,2,2])\n",
    "print(X.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3ad22cb4",
   "metadata": {},
   "outputs": [],
   "source": [
    "class MultiClassDNN(nn.Module):\n",
    "    def __init__(self):\n",
    "        super().__init__()\n",
    "        self.fc1=nn.Linear(2,8)\n",
    "        self.fc2=nn.Linear(8,2)\n",
    "        self.fc3=nn.Linear(2,3)\n",
    "        for m in self.modules():\n",
    "            if isinstance(m,nn.Linear):\n",
    "                nn.init.kaiming_uniform_(m.weight,a=math.sqrt(5))\n",
    "                print(\"m.weight=\",m.weight)\n",
    "                if m.bias is not None:\n",
    "                    fan_in,_=nn.init._calculate_fan_in_and_fan_out(m.weight)\n",
    "                    bound=1/math.sqrt(fan_in)\n",
    "                    nn.init.uniform_(m.bias,-bound,bound)\n",
    "                    \n",
    "                    \n",
    "\n",
    "    def forward(self,x):\n",
    "        x=f.relu(self.fc1(x))\n",
    "        x=f.relu(self.fc2(x))\n",
    "        x=self.fc3(x)\n",
    "        return x\n",
    "# model=MultiClassDNN()\n",
    "# print(model)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f65b8aaa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "torch.Size([8, 2])\n",
      "m.weight= Parameter containing:\n",
      "tensor([[-0.0562,  0.0430],\n",
      "        [-0.2424, -0.0053],\n",
      "        [-0.4299, -0.2239],\n",
      "        [ 0.0960,  0.6944],\n",
      "        [ 0.1409,  0.0334],\n",
      "        [ 0.2088, -0.5174],\n",
      "        [ 0.0564,  0.1727],\n",
      "        [ 0.0038,  0.6663]], requires_grad=True)\n",
      "m.weight= Parameter containing:\n",
      "tensor([[-0.1071,  0.1700,  0.0663, -0.1710,  0.0708,  0.2995,  0.2467,  0.1641],\n",
      "        [ 0.2690, -0.0928,  0.2344, -0.1823,  0.2346, -0.3264, -0.1802, -0.0410]],\n",
      "       requires_grad=True)\n",
      "m.weight= Parameter containing:\n",
      "tensor([[ 0.6006,  0.3089],\n",
      "        [ 0.4480, -0.3973],\n",
      "        [-0.6018,  0.6918]], requires_grad=True)\n",
      "MultiClassDNN(\n",
      "  (fc1): Linear(in_features=2, out_features=8, bias=True)\n",
      "  (fc2): Linear(in_features=8, out_features=2, bias=True)\n",
      "  (fc3): Linear(in_features=2, out_features=3, bias=True)\n",
      ")\n",
      "EPOCH0,Loss:1.4645\n",
      "model.fc1.weight= Parameter containing:\n",
      "tensor([[-0.0562,  0.0430],\n",
      "        [-0.2324,  0.0047],\n",
      "        [-0.4399, -0.2339],\n",
      "        [ 0.0860,  0.7044],\n",
      "        [ 0.1509,  0.0234],\n",
      "        [ 0.2188, -0.5074],\n",
      "        [ 0.0664,  0.1827],\n",
      "        [ 0.0138,  0.6563]], device='cuda:0', requires_grad=True)\n",
      "model.fc1.bias= Parameter containing:\n",
      "tensor([-0.6231,  0.4173,  0.4852,  0.3075,  0.0336, -0.5553, -0.4013,  0.1902],\n",
      "       device='cuda:0', requires_grad=True)\n",
      "EPOCH200,Loss:0.0039\n",
      "model.fc1.weight= Parameter containing:\n",
      "tensor([[-0.0562,  0.0430],\n",
      "        [-0.3441,  0.1354],\n",
      "        [-0.9527, -0.5375],\n",
      "        [ 0.2524,  0.3477],\n",
      "        [ 0.3138, -0.4432],\n",
      "        [ 0.3283, -0.3220],\n",
      "        [ 1.2198,  1.2980],\n",
      "        [ 0.2415,  1.1737]], device='cuda:0', requires_grad=True)\n",
      "model.fc1.bias= Parameter containing:\n",
      "tensor([-0.6231,  1.3151,  1.2894, -0.0991,  1.3582, -0.7553, -0.8319,  1.5266],\n",
      "       device='cuda:0', requires_grad=True)\n",
      "EPOCH400,Loss:0.0008\n",
      "model.fc1.weight= Parameter containing:\n",
      "tensor([[-0.0562,  0.0430],\n",
      "        [-0.3671,  0.1317],\n",
      "        [-0.9586, -0.5352],\n",
      "        [ 0.2997,  0.3868],\n",
      "        [ 0.2830, -0.4569],\n",
      "        [ 0.3283, -0.3220],\n",
      "        [ 1.3403,  1.4196],\n",
      "        [ 0.3352,  1.1789]], device='cuda:0', requires_grad=True)\n",
      "model.fc1.bias= Parameter containing:\n",
      "tensor([-0.6231,  1.4407,  1.3190, -0.0871,  1.5353, -0.7553, -0.8929,  1.7697],\n",
      "       device='cuda:0', requires_grad=True)\n",
      "EPOCH600,Loss:0.0003\n",
      "model.fc1.weight= Parameter containing:\n",
      "tensor([[-0.0562,  0.0430],\n",
      "        [-0.3784,  0.1319],\n",
      "        [-0.9614, -0.5339],\n",
      "        [ 0.3227,  0.4049],\n",
      "        [ 0.2661, -0.4626],\n",
      "        [ 0.3283, -0.3220],\n",
      "        [ 1.3962,  1.4728],\n",
      "        [ 0.3759,  1.1809]], device='cuda:0', requires_grad=True)\n",
      "model.fc1.bias= Parameter containing:\n",
      "tensor([-0.6231,  1.4972,  1.3314, -0.0822,  1.6162, -0.7553, -0.9200,  1.8841],\n",
      "       device='cuda:0', requires_grad=True)\n",
      "EPOCH800,Loss:0.0002\n",
      "model.fc1.weight= Parameter containing:\n",
      "tensor([[-0.0562,  0.0430],\n",
      "        [-0.3861,  0.1321],\n",
      "        [-0.9632, -0.5329],\n",
      "        [ 0.3380,  0.4169],\n",
      "        [ 0.2545, -0.4664],\n",
      "        [ 0.3283, -0.3220],\n",
      "        [ 1.4326,  1.5072],\n",
      "        [ 0.4022,  1.1814]], device='cuda:0', requires_grad=True)\n",
      "model.fc1.bias= Parameter containing:\n",
      "tensor([-0.6231,  1.5337,  1.3392, -0.0789,  1.6690, -0.7553, -0.9371,  1.9597],\n",
      "       device='cuda:0', requires_grad=True)\n",
      "EPOCH1000,Loss:0.0001\n",
      "model.fc1.weight= Parameter containing:\n",
      "tensor([[-0.0562,  0.0430],\n",
      "        [-0.3916,  0.1325],\n",
      "        [-0.9645, -0.5323],\n",
      "        [ 0.3495,  0.4259],\n",
      "        [ 0.2457, -0.4692],\n",
      "        [ 0.3283, -0.3220],\n",
      "        [ 1.4597,  1.5325],\n",
      "        [ 0.4218,  1.1813]], device='cuda:0', requires_grad=True)\n",
      "model.fc1.bias= Parameter containing:\n",
      "tensor([-0.6231,  1.5607,  1.3449, -0.0764,  1.7082, -0.7553, -0.9496,  2.0164],\n",
      "       device='cuda:0', requires_grad=True)\n",
      "EPOCH1200,Loss:0.0001\n",
      "model.fc1.weight= Parameter containing:\n",
      "tensor([[-0.0562,  0.0430],\n",
      "        [-0.3961,  0.1328],\n",
      "        [-0.9655, -0.5317],\n",
      "        [ 0.3588,  0.4332],\n",
      "        [ 0.2384, -0.4715],\n",
      "        [ 0.3283, -0.3220],\n",
      "        [ 1.4813,  1.5527],\n",
      "        [ 0.4373,  1.1810]], device='cuda:0', requires_grad=True)\n",
      "model.fc1.bias= Parameter containing:\n",
      "tensor([-0.6231,  1.5822,  1.3494, -0.0744,  1.7395, -0.7553, -0.9594,  2.0619],\n",
      "       device='cuda:0', requires_grad=True)\n",
      "EPOCH1400,Loss:0.0001\n",
      "model.fc1.weight= Parameter containing:\n",
      "tensor([[-0.0562,  0.0430],\n",
      "        [-0.3998,  0.1330],\n",
      "        [-0.9664, -0.5312],\n",
      "        [ 0.3666,  0.4394],\n",
      "        [ 0.2323, -0.4735],\n",
      "        [ 0.3283, -0.3220],\n",
      "        [ 1.4992,  1.5695],\n",
      "        [ 0.4503,  1.1807]], device='cuda:0', requires_grad=True)\n",
      "model.fc1.bias= Parameter containing:\n",
      "tensor([-0.6231,  1.6000,  1.3531, -0.0728,  1.7656, -0.7553, -0.9675,  2.1002],\n",
      "       device='cuda:0', requires_grad=True)\n",
      "EPOCH1600,Loss:0.0000\n",
      "model.fc1.weight= Parameter containing:\n",
      "tensor([[-0.0562,  0.0430],\n",
      "        [-0.4030,  0.1333],\n",
      "        [-0.9671, -0.5309],\n",
      "        [ 0.3734,  0.4447],\n",
      "        [ 0.2269, -0.4752],\n",
      "        [ 0.3283, -0.3220],\n",
      "        [ 1.5148,  1.5839],\n",
      "        [ 0.4615,  1.1802]], device='cuda:0', requires_grad=True)\n",
      "model.fc1.bias= Parameter containing:\n",
      "tensor([-0.6231,  1.6153,  1.3562, -0.0713,  1.7881, -0.7553, -0.9744,  2.1333],\n",
      "       device='cuda:0', requires_grad=True)\n",
      "EPOCH1800,Loss:0.0000\n",
      "model.fc1.weight= Parameter containing:\n",
      "tensor([[-0.0562,  0.0430],\n",
      "        [-0.4058,  0.1335],\n",
      "        [-0.9678, -0.5305],\n",
      "        [ 0.3793,  0.4494],\n",
      "        [ 0.2221, -0.4767],\n",
      "        [ 0.3283, -0.3220],\n",
      "        [ 1.5284,  1.5965],\n",
      "        [ 0.4714,  1.1798]], device='cuda:0', requires_grad=True)\n",
      "model.fc1.bias= Parameter containing:\n",
      "tensor([-0.6231,  1.6288,  1.3590, -0.0700,  1.8079, -0.7553, -0.9804,  2.1626],\n",
      "       device='cuda:0', requires_grad=True)\n",
      "Predicted tensor([0, 0, 1, 1, 2, 2, 2, 2], device='cuda:0')\n",
      "cuda:0\n",
      "cuda:0\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "import numpy as np\n",
    "from torch import nn\n",
    "import torch.nn.functional as f\n",
    "import math\n",
    "device=torch.device(\"cuda\" if torch.cuda.is_available()else \"cpu\")\n",
    "print(torch.cuda.is_available())\n",
    "X=torch.tensor([[1., 2.],\n",
    "                [2., 1.],\n",
    "                [3., 5.],\n",
    "                [5., 3.],\n",
    "                [1., -1.],\n",
    "                [-1., 1.],\n",
    "                [-2., -3.],\n",
    "                [-3., -2.]])\n",
    "X=X.to(device) \n",
    "Y=torch.tensor([0,0,1,1,2,2,2,2])\n",
    "Y=Y.to(device)\n",
    "print(X.shape)\n",
    "class MultiClassDNN(nn.Module):\n",
    "    def __init__(self):\n",
    "        super().__init__()\n",
    "        self.fc1=nn.Linear(2,8)\n",
    "        self.fc2=nn.Linear(8,2)\n",
    "        self.fc3=nn.Linear(2,3)\n",
    "        for m in self.modules():\n",
    "            if isinstance(m,nn.Linear):\n",
    "                nn.init.kaiming_uniform_(m.weight,a=math.sqrt(5))\n",
    "                print(\"m.weight=\",m.weight)\n",
    "                if m.bias is not None:\n",
    "                    fan_in,_=nn.init._calculate_fan_in_and_fan_out(m.weight)\n",
    "                    bound=1/math.sqrt(fan_in)\n",
    "                    nn.init.uniform_(m.bias,-bound,bound)\n",
    "                    \n",
    "                    \n",
    "\n",
    "    def forward(self,x):\n",
    "        x=f.relu(self.fc1(x))\n",
    "        x=f.relu(self.fc2(x))\n",
    "        x=self.fc3(x)\n",
    "        return x\n",
    "model=MultiClassDNN().to(device)\n",
    "print(model)\n",
    "criterion=nn.CrossEntropyLoss()\n",
    "optimizer=torch.optim.Adam(model.parameters(),lr=0.01)\n",
    "\n",
    "for epoch in range(2000):\n",
    "    output=model(X)\n",
    "    loss=criterion(output,Y)\n",
    "    optimizer.zero_grad()\n",
    "    loss.backward()\n",
    "    optimizer.step()\n",
    "    if epoch%200==0:\n",
    "        print(f\"EPOCH{epoch},Loss:{loss.item():.4f}\")\n",
    "        print(\"model.fc1.weight=\",model.fc1.weight)\n",
    "        print(\"model.fc1.bias=\",model.fc1.bias)\n",
    "        # print(\"model.fc2.weight=\",model.fc2.weight)\n",
    "        # print(\"model.fc2.bias=\",model.fc2.bias)\n",
    "        # print(\"model.fc3.weight=\",model.fc3.weight)\n",
    "        # print(\"model.fc3.bias=\",model.fc3.bias)\n",
    "        # print(\"X=\",X)\n",
    "        # print(\"Y=\",Y)\n",
    "        # print(\"output=\",output)\n",
    "        # print(\"pred_classes=\",output.argmax(dim=1))\n",
    "      \n",
    "with torch.no_grad():\n",
    "    logits=model(X)\n",
    "    pred_classes=logits.argmax(dim=1)\n",
    "    print(\"Predicted\",pred_classes)\n",
    "print(model.fc1.weight.device)#模型权重所在设备\n",
    "print(X.device)#显示输入数据所在设备\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "18919c15",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
